package waal.pc.frontend.config;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import waal.pc.backend.hub.Hub;
import waal.pc.backend.module.Module;
import waal.pc.frontend.Surface;

import waal.pc.frontend.tiles.ModuleTile;
import waal.pc.frontend.tiles.TileHandler;

public class ModuleArea extends JSplitPane implements ListSelectionListener, FocusListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -613854608577708118L;
	
	private WaalConfig wc;
	private JPanel buttonPanel;
	private GridBagLayout bgbl;
	private JLabel jLheadline;
	private JLabel jLModuleNumber;
	private JLabel jLModuleName;
	private JButton activeButton;
	private JList<Module> moduleList;
	private Map<Hub, ModuleList> modules;
	private ModuleList activeListModel = null;
	private DecimalFormat df = new DecimalFormat("##0.00");
	private JLabel jLModuleMax;
	private JLabel jLModuleMin;
	private JLabel jLModuleBits;
	private JLabel jLModuleTimeBase;
	private JLabel jLModuleUnit;
	private JLabel jLModuleDigital;
	private TileHandler tileHandler;

	private JComboBox<String> tileBox;

	private JButton addTileButton;

	private JComboBox<Integer> tileNumberBox;


	public ModuleArea(WaalConfig wc, TileHandler tileHandler) { //, Hub hub) {
		super(JSplitPane.VERTICAL_SPLIT);
		this.wc = wc;
		this.tileHandler = tileHandler;
		this.modules = new HashMap<Hub, ModuleList>();
		this.setOneTouchExpandable(true);
		this.setDividerLocation(250);
		this.setLeftComponent(this.makeTopPanel());
		this.setRightComponent(this.makeButtonPanel());
		
	}
	
	public void makeModulesForHub(Hub hub) {
		if(!this.modules.containsKey(hub)) {
			this.modules.put(hub, new ModuleList(this, hub));	
		}
	}
	
	public void makeChannelsForModule(Module module) {
		this.wc.makeChannelsForModule(module);
	}
	
	public void setActiveHub(Hub hub) {
		if(this.activeListModel != null) {
			this.activeListModel.setLastIndex(this.moduleList.getSelectedIndex());
			
		}
		this.activeListModel = this.modules.get(hub);
		this.moduleList.setModel(this.activeListModel);
		this.moduleList.setSelectedIndex(this.activeListModel.getLastIndex());
		if(this.moduleList.getSelectedValue() != null) {
			this.wc.setActiveModule(this.moduleList.getSelectedValue());
		}
	}
	
	private JPanel makeTopPanel() {
		JPanel topPane = new JPanel();
		topPane.setLayout(new BoxLayout(topPane, BoxLayout.Y_AXIS));
		topPane.add(new JLabel("Module Liste"));
		this.moduleList = new JList<Module>();
		this.moduleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.moduleList.addListSelectionListener(this);
		this.moduleList.addFocusListener(this);
		this.moduleList.setMaximumSize(new Dimension(200,1000));
		this.moduleList.setMinimumSize(new Dimension(100,100));
		this.moduleList.setCellRenderer(new ModuleListCellRenderer());
		topPane.add(this.moduleList);
		return topPane;
	}
	
	private JPanel makeButtonPanel() {
		this.bgbl = new GridBagLayout();
		this.buttonPanel = new JPanel(this.bgbl);
		//this.buttonPanel.setMinimumSize(new Dimension(200,200));
		this.jLheadline = new JLabel();
		this.jLModuleNumber = new JLabel();
		this.jLModuleName = new JLabel();
		this.jLModuleMax = new JLabel();
		this.jLModuleMin = new JLabel();
		this.jLModuleBits = new JLabel();
		this.jLModuleTimeBase = new JLabel();
		this.jLModuleUnit = new JLabel();
		this.jLModuleDigital = new JLabel();
		
		this.tileNumberBox = new JComboBox<Integer>();
		for(int i = 1; i <= 6; i ++) {
			this.tileNumberBox.addItem(i);
		}
		
		this.tileBox = new JComboBox<String>();
		ActionListener alTileComboChange = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(tileBox.getSelectedItem() != null) 
				addTileButton.setText("Set Tile: " + tileBox.getSelectedItem().toString() + " to Position: " + tileNumberBox.getSelectedItem());
			}
		};
		this.tileNumberBox.addActionListener(alTileComboChange);
		this.tileBox.addActionListener(alTileComboChange);

		this.addTileButton = new JButton("Add");
		ActionListener alAddTile = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(tileBox.getSelectedItem() != null) {
					ModuleTile t = (ModuleTile) tileHandler.getTile(true, (String) tileBox.getSelectedItem());
					wc.addTileToSurface(t.getTile(moduleList.getSelectedValue()), (int)tileNumberBox.getSelectedItem());
					
				}
			}
		};
		this.addTileButton.addActionListener(alAddTile);
		
		
		this.activeButton = new JButton("active");
		ActionListener alActive = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
			}
		};
		this.activeButton.addActionListener(alActive);
		this.activeButton.setEnabled(false);
		
		//															x, y, width, height, weightx, weighty )
		this.addButtonComponent(this.jLheadline, 					0, 0, 2, 1, 1.0, 0.0);
		this.addButtonComponent(new JLabel("Number:"),				0, 1, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLModuleNumber, 				1, 1, 1, 1, 1.0, 0.0);
		this.addButtonComponent(new JLabel("Name:"),				0, 2, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLModuleName,	 				1, 2, 1, 1, 1.0, 0.0);
		this.addButtonComponent(new JLabel("Bits:"),				0, 3, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLModuleBits, 					1, 3, 1, 1, 1.0, 0.0);
		this.addButtonComponent(new JLabel("Maximum:"),				0, 4, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLModuleMax,	 				1, 4, 1, 1, 1.0, 0.0);
		this.addButtonComponent(new JLabel("Minimum:"),				0, 5, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLModuleMin, 					1, 5, 1, 1, 1.0, 0.0);
		this.addButtonComponent(new JLabel("Einheit:"),				0, 6, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLModuleUnit, 					1, 6, 1, 1, 1.0, 0.0);
		this.addButtonComponent(new JLabel("TimeBase:"),			0, 7, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLModuleTimeBase, 				1, 7, 1, 1, 1.0, 0.0);
		this.addButtonComponent(new JLabel("Digital:"),				0, 8, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLModuleDigital, 				1, 8, 1, 1, 1.0, 0.0);
		this.addButtonComponent(this.tileNumberBox,	 					0, 9, 1, 1, 1.0, 0.0);
		this.addButtonComponent(this.tileBox,	 						1, 9, 1, 1, 1.0, 0.0);
		this.addButtonComponent(this.addTileButton,	 				0,10, 2, 1, 1.0, 0.0);
		this.addButtonComponent(this.activeButton,	 				0,11, 2, 1, 1.0, 0.0);
		return this.buttonPanel;
	}
	
	public void updateButton(Module module) {
		this.jLheadline.setText("Module: " + module.getNumber());
		this.jLModuleNumber.setText("" + module.getNumber());
		this.jLModuleName.setText(module.getName());
		this.tileBox.setModel(this.tileHandler.getTileList(module.isDigital(), true, module.hasInput(), module.hasOutput()));
		this.jLModuleBits.setText("" + module.getBits());
		this.jLModuleMax.setText(this.df.format(module.getMax()) + module.getUnit());
		this.jLModuleMin.setText(this.df.format(module.getMin()) + module.getUnit());
		this.jLModuleUnit.setText(module.getUnit());
		this.jLModuleTimeBase.setText(new TimeUnitListElement(module.getMinimalTimeBase()).toString());
		
		if(module.isDigital()) {
			this.jLModuleDigital.setText("ja");
		} else {
			this.jLModuleDigital.setText("nein");
		}
		
		if(module.getHub().isMaster()) {
			this.activeButton.setEnabled(true);
			if(module.isActive()) {
				this.activeButton.setText("Active");		
			} else {
				this.activeButton.setText("Deaktive");
			}
		} else {
			this.activeButton.setEnabled(false);
		}
		
	}
	
	
	private void addButtonComponent(Component c, int x, int y, int width, int height, double weightx, double weighty ) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = x; gbc.gridy = y;
		gbc.gridwidth = width; gbc.gridheight = height;
		gbc.weightx = weightx; gbc.weighty = weighty;
		gbc.ipadx = 10; gbc.ipady = 5;
		this.bgbl.setConstraints( c, gbc );
		c.setPreferredSize(new Dimension(Surface.tile_x, Surface.tile_y));
		this.buttonPanel.add( c );
		//this.surface.pack();
	}
	
	@Override
	public void focusGained(FocusEvent arg0) {
		if(this.moduleList.getSelectedValue() != null) {
			this.updateButton(this.moduleList.getSelectedValue());
		}
		
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if(!e.getValueIsAdjusting()) {
			return;
		} else {
			Module module = this.moduleList.getSelectedValue();
			this.updateButton(module);
			this.wc.setActiveModule(module);
		}	
	}
	
	public void updateFreeTileNumber(int tile) {
		this.tileNumberBox.setSelectedIndex(tile-1);
	}
	
	
}
